package com.ibeifeng.bigdata.senior.hadoop.hdfs;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FSDataInputStream;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IOUtils;

public class HdfsApp {

	/**
	 * Get FileSystem
	 * 
	 * @return
	 * @throws Exception
	 */
	public static FileSystem getFileSystem() throws Exception {

		// create configuration ,core-*.xml and hdfs-*.xml
		// *-default.xml
		Configuration configuration = new Configuration();

		// get file system
		FileSystem fileSystem = FileSystem.get(configuration);

		// System.out.println(fileSystem);

		return fileSystem;
	}
	
	
	public static void read(String fileName)throws Exception {
		
		// get file system
		FileSystem fileSystem = getFileSystem();
		
		//  bin/hdfs dfs -text /user/beifeng/tmp/core-site.xml
		// read Path
		Path readPath = new Path(fileName);
		
		// read file ,open input stream
		FSDataInputStream inStream = fileSystem.open(readPath);
		
		try{
			// read 
			IOUtils.copyBytes(inStream, System.out, 4096, false);
		}catch(Exception e){
			e.printStackTrace();
		}finally{
			// close steam
			IOUtils.closeStream(inStream);
		}
		
	}
	

	public static void main(String[] args) throws Exception {
/**		
		// read file
		String fileName = "/user/beifeng/tmp/core-site.xml" ;*/
		String fileName = "/user/beifeng/tmp/wc.input" ;
		read(fileName);

/**
		//  bin/hdfs dfs -put /user/beifeng/tmp/core-site.xml
		
		// get file system
		FileSystem fileSystem = getFileSystem();
		
		String fileName = "/user/beifeng/tmp/wc.input" ;
		
		// read Path
		Path writePath = new Path(fileName);
		
		// write file 
		FSDataOutputStream outStream = fileSystem.create(writePath);
		
		// get input Stream
		FileInputStream inStream = new FileInputStream(//
			new File("/opt/datas/wc.input")//
		) ;
		
		try{
			// read 
			IOUtils.copyBytes(inStream, outStream, 4096, false);
		}catch(Exception e){
			e.printStackTrace();
		}finally{
			// close steam
			IOUtils.closeStream(outStream);
			IOUtils.closeStream(inStream);
		}
		
*/		
		
		
		
	}
	
}
